﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TERP.Constants;
using TERP.WaterClient.Common;
using TERP.WaterClient.DbServices;
using CommonUtils;

namespace TERP.WaterClient.Views.Water
{
    public partial class FrmWaterPayCover : FrmMdiBase
    {
        #region Properties
        public delegate void SendValue(int _noVo);
        public SendValue _sendValue = null;
		//public ITS.Library.WaterCustomerService.WaterCustomer _waterCustomer;

		//public ITS.Library.WaterReceiptService.WaterReceipt _receiptWater;
		//ITS.Library.WaterReceiptService.WaterReceiptServiceClient _clientWaterReceipt;
        private bool isclose=false;

		private string _mStatus = ActionMyStatus.Add;
		ReceiptService _receiptService;
		private string strCusID = string.Empty;
		private string strReceiptID = string.Empty;

        #endregion

        #region Method
        void Save()
        {
			_receiptService = new ReceiptService();
			//Add
			if (_mStatus == ActionMyStatus.Add)
			{
				_receiptService.Its_Receipt_Create(
													 this.GetTextBoxStringValue(txtBillCode)
													 , this.GetDateEditStringValue(dedDate, Nonsense.DATE_DB_FORMAT)
													 , txtNote.Text
													 , strCusID
													 , this.GetTextBoxInt32Value(txtAmountPay).ToString()
													 , ReceiptType.ReceiptType_TraVo
													 );
			}

            //Update
            if (_mStatus==ActionMyStatus.Edit)
            {
				_receiptService.Its_Receipt_Update(
														strReceiptID
													 ,this.GetTextBoxStringValue(txtBillCode)
													 , this.GetDateEditStringValue(dedDate, Nonsense.DATE_DB_FORMAT)
													 , txtNote.Text
													 , strCusID
													 , this.GetTextBoxInt32Value(txtAmountPay).ToString()
													 , ReceiptType.ReceiptType_TraVo
													 );
            }

			//if (_sendValue!=null)
			//{
			//    _sendValue(_waterCustomer.NoVoCurrent);
			//}
            if (isclose)
            {
                this.Close();
            }
        }

        bool CheckInPut()
        {
            if (txtName.Text == "")
            {
                Util.ShowWarningMsgBox("Nhập tên ");
                txtName.Focus();
                return false;
            }
            if (StringUtils.IsEmpty(txtAmountCurrent.Text))
            {
                Util.ShowWarningMsgBox("Nhập số vỏ trả");
                txtAmountCurrent.Focus();
                return false;
            }

            return true;
        }

        void BindDataToControl(DataRow drow)
        {
            //txtAddress.Text = _cus.SupAddress;
            //txtBillCode.Text = _cus.BillCode;
            //txtName.Text = _cus.SupName;
            //txtNote.Text = _cus.Note;
            ////txtOrgName.Text = _cus.OrgName;
            ////txtName.Text = _cus.CusTel;
            ////txtDebit.EditValue = _cus.Debit;
            //txtAmountCurrent.EditValue = _cus.Amount;
        }

        void CleanData()
        {
            txtAmountCurrent.EditValue = 0;
            //txtAddress.Text = "";
            txtBillCode.Text = "";
            txtNote.Text = "";
            txtName.Text = "";
        }
        #endregion 

        #region Event

        public FrmWaterPayCover()
        {
            InitializeComponent();
        }

		public FrmWaterPayCover(string _strCusID, string _strCusName, string _strDebit)
		{
			InitializeComponent();
			this.strCusID = _strCusID;
			this.txtName.Text = _strCusName;
			this.txtAmountCurrent.EditValue = _strDebit;
		}

        public FrmWaterPayCover(bool IsLoadFromBooking)
        {
            InitializeComponent();
            this.isclose = IsLoadFromBooking;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInPut())
                {
                    Save();
                    Util.ShowMessageBox("Đã lưu thông tin phiếu thu");
                }
            }
            catch (Exception ex)
            {
                Util.ShowExceptionBox(ex);
            }
        }

        private void FrmCustomer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                try
                {
                    if (CheckInPut())
                    {
                        Save();
                        Util.ShowMessageBox("Đã lưu thông tin phiếu thu");
                    }
                }
                catch (Exception ex)
                {
                    Util.ShowExceptionBox(ex);
                }
            }
        }

        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            txtBillCode.Text = Util.SinhMa("TV");
            dedDate.EditValue = DateTime.Now;
            //txtAddress.Text = _waterCustomer.Address;
			//if (_waterCustomer!=null)
			//{
			//    txtAmountCurrent.EditValue = _waterCustomer.NoVoCurrent;
			//    txtName.Text = _waterCustomer.CusName;
			//}            

			//if (_mStatus == StatusEdit.StatusEditForm.Update)
			//{
			//    BindDataToControl(_receiptWater);
			//}
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (Util.ShowMsgBoxWarningQuestion("Bạn muốn xóa thông tin trả vỏ???"))
                {
					_receiptService = new ReceiptService();
					_receiptService.Its_Receipt_Delete(strReceiptID);
                    CleanData();
                }
            }
            catch (Exception ex)
            {
                Util.ShowExceptionBox(ex);
            }
        }

        private void txtAmountPay_EditValueChanged(object sender, EventArgs e)
        {
            int _Novo = 0;
			//if (_waterCustomer!=null)
			//{
			//    _Novo = _waterCustomer.NoVoCurrent;
			//    txtAmountCurrent.EditValue = _Novo - Convert.ToInt32(txtAmountPay.EditValue);
			//}            
        }

        #endregion
    }
}
